package org.xqyy.eval.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xqyy.eval.model.Question;
import org.xqyy.eval.model.TQuestion;
import org.xqyy.eval.model.TestPaper;
import org.xqyy.eval.service.IQuestionService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-24.
 */
@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    @RequestMapping("/loadQuestions")
    @ResponseBody
    public Map<String, Object> loadQuestions(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> user = null;
        user = (Map<String, Object>)request.getSession().getAttribute("user");
        Map<String, Object> res = new HashMap<>();
        /*List<Map<String, Object>>  list = questionService.getpaperId((String) user.get("deptCode"));
        if (list.size() >0 ){
            Map<String, Object> objectMap = list.get(0);
            res.put("paperId", ((Integer)objectMap.get("paperId")).toString());
        }*/
        String filter = request.getParameter("filter");
        if (StringUtils.hasLength(filter)) {
            res.put("filter", filter);
        }
        String deptCode = request.getParameter("deptCode");
        if (StringUtils.hasLength(deptCode)) {
            res.put("deptCode", deptCode);
        } else {
            user = (Map<String, Object>) request.getSession().getAttribute("user");
            res.put("deptCode", user.get("deptCode"));
        }
        String groupCode = request.getParameter("groupCode");
        if (StringUtils.hasLength(groupCode)) {
            res.put("groupCode", groupCode);
        }
        String paperId = request.getParameter("paperId");
        if (StringUtils.hasLength(paperId)) {
            res.put("paperId", paperId);
        }


        List<Map<String, Object>> data = questionService.load(res);
        res.put("root", data);
        return res;
    }

    @RequestMapping("/getpaperId")
    @ResponseBody
    public Map<String, Object> getpaperId(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> user = null;
        user = (Map<String, Object>) request.getSession().getAttribute("user");
        Map<String, Object> res = new HashMap<>();
        List<Map<String, Object>>  list = questionService.getpaperId((String) user.get("deptCode"));
        if (list.size() >0 ){
            Map<String, Object> objectMap = list.get(0);
            res.put("paperId", ((Integer)objectMap.get("paperId")).toString());
        }
        return res;
    }


    @RequestMapping("/addQuestion")
    @ResponseBody
    public Map<String, Object> addQuestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        try {
            String type = request.getParameter("type");
            String content = request.getParameter("content");
            String mark = request.getParameter("mark");
            res.put("type", type);
            res.put("content", content);
            res.put("mark", mark);
            questionService.add(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/updateQuestion")
    @ResponseBody
    public Map<String, Object> updateQuestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        try {
            String id = request.getParameter("id");
            String type = request.getParameter("type");
            String content = request.getParameter("content");
            String mark = request.getParameter("mark");
            res.put("id", id);
            if (StringUtils.hasLength(type)) {
                res.put("type", type);
            }
            if (StringUtils.hasLength(content)) {
                res.put("content", content);
            }
            if (StringUtils.hasLength(mark)) {
                res.put("mark", mark);
            }
            questionService.modify(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/deleteQuestion")
    @ResponseBody
    public Map<String, Object> deleteQuestion(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> res = new HashMap();
        try {
            String id = request.getParameter("id");
            res.put("id", id);
            questionService.delete(res);
            res.put("success", true);
        } catch (Exception e) {
            res.put("failure", e.getMessage());
            e.printStackTrace();
        }
        return res;
    }

    @RequestMapping("/selectQuestionByPaperId")
    @ResponseBody
    public List<TQuestion> selectQuestionByPaperId(TestPaper testPaper) {
        return questionService.selectQuestionByPaperId(testPaper.getPaperId());
    }

    @RequestMapping("/selectAllQuestion")
    @ResponseBody
    public List<TQuestion> selectAllQuestion() {
        return questionService.selectAllQuestion();
    }

    @RequestMapping("/addTQuestion")
    @ResponseBody
    public Boolean addTQuestion(TQuestion tQuestion) {
        try {
            questionService.addQuestion(tQuestion);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/findQuestionById")
    @ResponseBody
    public TQuestion findQuestionById(TQuestion tQuestion) {
        try {
            tQuestion = questionService.findQuestionById(tQuestion);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tQuestion;
    }

    @RequestMapping("/updateTQuestion")
    @ResponseBody
    public Boolean updateTQuestion(TQuestion tQuestion) {
        try {
            TQuestion tempQuestion = questionService.findQuestionById(tQuestion);
            tempQuestion.setContent(tQuestion.getContent());
            tempQuestion.setMark(tQuestion.getMark());
            questionService.updateQuestion(tempQuestion);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
